package com.xiaobai.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * @author yangdaji
 * @version 1.0
 * @Description: 实现用户接口, 自定义登录逻辑
 * @date 22-2-19 上午 1:35
 */
@Service
public class UserServiceImpl implements UserDetailsService {

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        String encode = passwordEncoder.encode("12345");
        if (!"admin".equals(username)) {
            throw new UsernameNotFoundException("用户不存在!");
        }
        return new User(username, encode, AuthorityUtils.commaSeparatedStringToAuthorityList("admin,normal"));
    }
}
